package com.example.administrator.xiangce;
import android.Manifest;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<Object>list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        //MyAdapter
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        ListView listView = (ListView) findViewById(R.id.listView);
         MyAdapter adapter = new MyAdapter(this, getList());
        listView.setAdapter(adapter);

    }

    private ArrayList<Object> getList() {

        //ArrayList<Object> list = new ArrayList<>();

        String[] columns = {MediaStore.Images.Thumbnails.DATA,
                MediaStore.Images.Thumbnails._ID};

        Cursor cursor = this.getApplicationContext()
                .getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        columns, null, null, null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);

            String path = cursor.getString(dataColumnIndex);

            Object object = new Object();
            object.setTuPian(path);
           // object.setZhangShu(path);
            list.add(object);
        }
        return list;

    }
}